﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Configuration;

using Microsoft.ECSS.Common.CommonServiceContract;
using Microsoft.ECSS.Common.Function;

namespace Koonsoft.Tool.SendMessage
{
    /// <summary>
    /// 调用短信平台WCF的助手类
    /// </summary>
    public class SMSHelper
    {
        private IShortMessage _client = null;

        public SMSHelper()
        {
            try
            {
                BasicHttpBinding ntb = new BasicHttpBinding();
                string SMSServiceUrl = ConfigurationManager.AppSettings["SMSWCFServiceUrl"];

                var factory = new ChannelFactory<IShortMessage>(ntb, SMSServiceUrl);
                factory.Endpoint.Contract.SessionMode = SessionMode.Allowed;
                _client = factory.CreateChannel();
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        /// <summary>
        /// 使用模板发送短信
        /// </summary>
        /// <param name="moduleInfo">模块描述</param>
        /// <param name="templetID">模板标识</param>
        /// <param name="dictionary">参数键值对</param>
        /// <param name="targetMobileNumbers">目标号码串</param>
        /// <param name="sendTime">发送时间</param>
        /// <param name="ipAddress">发送IP</param>
        /// <returns></returns>
        public bool SendShortMessageByTemplet(string moduleInfo, int templetID, Dictionary<string, string> dictionary, string targetMobileNumbers, string ipAddress, DateTime sendTime)
        {
            return _client.SendShortMessageByTemplet(moduleInfo, templetID, dictionary, targetMobileNumbers,
                                                          ipAddress, sendTime);
        }

        /// <summary>
        /// 不使用模板发送短信
        /// </summary>
        /// <param name="moduleInfo">模板描述</param>
        /// <param name="targetMobileNumbers">目标号码串</param>
        /// <param name="content">短信内容</param>
        /// <param name="ipAddress">发送IP</param>
        /// <param name="sendTime">发送时间</param>
        /// <returns></returns>
        public bool SendShortMessageWithoutTemplet(string moduleInfo, string targetMobileNumbers, string content, string ipAddress, DateTime sendTime)
        {
            return _client.SendShortMessageWithoutTemplet(moduleInfo, targetMobileNumbers, content, ipAddress, sendTime);
        }

        /// <summary>
        /// 发送短信验证码
        /// </summary>
        /// <param name="mobileNumber">目标号码</param>
        /// <param name="moduleDescription">模块描述（应用方的描述，验证需要传入）</param>
        /// <returns></returns>
        public bool SendIdentifyingCode(string mobileNumber, string moduleDescription)
        {
            return _client.SendIdentifyingCode(mobileNumber, moduleDescription);
        }

        /// <summary>
        /// 校验短信验证码
        /// </summary>
        /// <param name="mobileNumber">目标号码</param>
        /// <param name="moduleCode">模块描述</param>
        /// <param name="identifyingCode">验证码</param>
        public bool VerifyIdentifyingCode(string mobileNumber, string moduleCode, string identifyingCode)
        {
            return _client.VerifyIdentifyingCode(mobileNumber, moduleCode, identifyingCode);
        }
    }
}
